package org.test4j.mock.processor.filer.file;

import com.squareup.javapoet.ClassName;
import g_asm.org.objectweb.asm.*;
import org.test4j.mock.faking.meta.MethodId;
import org.test4j.mock.faking.util.ClassFile;
import org.test4j.mock.faking.util.TypeUtility;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;

import static org.test4j.mock.faking.modifier.FakeTransformer.notMockType;
import static org.test4j.mock.faking.util.AsmConstant.acceptOptions;
import static org.test4j.mock.faking.util.AsmConstant.api_code;
import static org.test4j.mock.faking.util.TypeUtility.isSynthetic;

public class MockClassFiler extends MockUpFiler {
    private Class clazz;

    public MockClassFiler(Class clazz) {
        super(ClassName.get(clazz));
        this.clazz = clazz;
        this.parserSuperClass();
        this.parseMethodIds();
        this.parseAbstractMethodIds();
    }

    protected void parseMethodIds() {
        ClassReader cr = ClassFile.getClassReader(clazz);
        cr.accept(new ParseMethodVisitor(this), acceptOptions);
    }

    private void parseAbstractMethodIds() {
        List<Method> interfaceMethods = TypeUtility.findInterfaceMethod(clazz);
        for (Method method : interfaceMethods) {
            super.addMethodId(method.getModifiers(), method.getName(), Type.getMethodDescriptor(method));
        }
    }

    protected void parserSuperClass() {
        Class superClass = clazz.getSuperclass();
        if (superClass == null) {
            return;
        }
        String fullName = clazz.getSuperclass().getName();
        if (!notMockType(fullName)) {
            super.superClass = fullName;
        }
    }
}